local houding = fk.CreateSkill{
  name = "lingling__houding",
}

Fk:loadTranslationTable{
  ["lingling__houding"] = "后定",
  [":lingling__houding"] = "每回合限一次，当你受到伤害后，你可以将一张红色牌当【韬光养晦】使用，伤害来源可以将两张黑色牌当【无懈可击】使用。",

  ["#lingling__houding-invoke"] = "后定：你可以将一张红色牌当【韬光养晦】使用",
  ["#lingling__houding_src-invoke"] = "后定：你可以将一张红色牌当【韬光养晦】使用，%dest 可以将两张黑色牌当【无懈可击】使用",

  ["#lingling__wangmeng_EasterEgg"] = "今长安咫尺，而不渡灞水，百姓未知公心。",
}

houding:addEffect(fk.Damaged, {
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(houding.name) and
      (not player:isNude() or #player:getHandlyIds() > 0)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local prompt = "#lingling__houding-invoke"
    if data.from and not data.from.dead then
      prompt = "#lingling__houding_src-invoke::"..data.from.id
    end
    local use = room:askToUseVirtualCard(player, {
      name = "ling__hide_capacities",
      skill_name = houding.name,
      prompt = prompt,
      cancelable = true,
      card_filter = {
        n = 1,
        pattern = ".|.|heart,diamond",
      },
      skip = true,
    })
    if use then
      event:setCostData(self, {extra_data = use})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if data.from and not data.from.dead then
      room:handleAddLoseSkills(data.from, "lingling__houding_viewas&", nil, false, true)
    end
    room:useCard(event:getCostData(self).extra_data)
    if data.from then
      room:handleAddLoseSkills(data.from, "-lingling__houding_viewas&", nil, false, true)
    end
  end,
})

houding:addEffect(fk.AfterCardTargetDeclared,{
  can_refresh = function (self, event, target, player, data)
    return table.contains(data.tos,player) and player.general == "lingling__wangmeng" and data.from.general == "lingling__huanwen" and player:getMark("lingling__wangmeng_EasterEgg") == 0 and data.card.trueName == "sincere_treat"
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player,"lingling__wangmeng_EasterEgg",1)
    room:sendLog{
      type = "#lingling__wangmeng_EasterEgg",
      from = player.id,
      toast = true,
    }
    data:cancelAllTarget()
  end,
})

return houding
